package com.pengtu.gsj.service.impl.check.lzgl.basEduca;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.lzgl.RoadInfoSysDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.RoadInfoSys;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.basEduca.RoadAdminInfoService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class RoadAdminInfoServiceImpl implements RoadAdminInfoService {

	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public RoadInfoSysDao roadInfoSysDao;
	
	@Override
	public Map<String, Object> getRoadAdminInfos(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<RoadInfoSys> roadInfoSys = new ArrayList<RoadInfoSys>();
		List<List<String>> resultLists = new ArrayList<List<String>>();
		List<String> checkResult = Arrays.asList("0", "0", "0");
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double percentage = 0; //完善率
		double total = 0; //计算总分
		double tableFull = yearFull;
		
		int[] years = new int[systemManageService.getCheckYear(endTime, startTime, 0)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.valueOf(startTime)+i;
		}
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		//查询检查时间内的所有的对象，并得到待处理的目标数据
		//根据开始时间和结束时间查询所有的数据文件
		roadInfoSys = getCheckData(startTime, endTime, true);
		if(null != roadInfoSys && 0 != roadInfoSys.size()){
			for (RoadInfoSys infoSys : roadInfoSys) {
				//定义个定长List分别保存系统是否建立，是否实际应用，是否实现网上办事或监督以及是否已经上传年度应用报告
				checkResult = Arrays.asList("0", "0", "0");
				if ("0".equals(checkResult.get(0))) {
					checkResult.set(0, "1");
				}
				if ("1".equals(infoSys.getIfImplNet()) && "0".equals(checkResult.get(1))) {
					checkResult.set(1, "1");
				}
				
				for (int i = 0; i < years.length; i++) {
					for (Attachment attachment : infoSys.getAttachments()) {
						if ("lzxxh_ndyybg".equals(attachment.getTag()) && years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", attachment.getFileTime()))) {
							checkResult.set(2, "1");
						}
					}
				}
				resultLists.add(checkResult);
			}
			
			//遍历目标数据集合并返回最终结果
			checkResult = Arrays.asList("0", "0", "1");
			for (List<String> tempList : resultLists) {
				for (int i = 0; i < 3; i++) {
					if ("1".equals(tempList.get(i)) && "0".equals(checkResult.get(i))) {
						checkResult.set(i, "1");
					}
				}
				if ("0".equals(tempList.get(2)) && "1".equals(checkResult.get(2))) {
					checkResult.set(2, "0");
				}
			}
		}
		if ("1".equals(checkResult.get(0)) && "1".equals(checkResult.get(1)) && "1".equals(checkResult.get(2))) {
			total = yearFull;
		} else if ("1".equals(checkResult.get(0)) && "1".equals(checkResult.get(1)) || "1".equals(checkResult.get(0)) && "1".equals(checkResult.get(2))) {
			total = MathsUtils.formatDouble(yearFull * 3 / 4, 1);
		} else if ("1".equals(checkResult.get(0)) &&  "0".equals(checkResult.get(1)) && "0".equals(checkResult.get(2))) {
			total = 1;
		} else {
			total = 0;
		}
		
		percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("manageData", roadInfoSys);
		map.put("checkResult", checkResult);
		map.put("percent", percentage);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
	
	/**
	 * getCheckData:
	 * 适用:根据startTime, endTime检索数据文件列表
	 * @param startTime = 项目开始时间
	 * @param endTime = 项目结束时间
	 * @param b = 是否升序
	 * @return 
	 * @exception 
	 * @since  1.0.0
	*/
	@Override
	public List<RoadInfoSys> getCheckData(String startTime, String endTime, boolean b) {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("year", startTime, MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("year",  endTime, MatchType.LE);
		}
		return roadInfoSysDao.find(queryFilter, "year", true);
	}
}